from odoo import models, fields, api
from odoo import exceptions # will be used in the code
class MeasWorkSubWizard(models.TransientModel):
    _name='meas.ctl.work.sub.wizard'
    _description="采集多数据表头"
    work_sub_id=fields.Many2one('meas.ctl.work.sub',string="工单明细")
    decimal=fields.Boolean('3位精度')
    min_value=fields.Float('允许最小值',related="work_sub_id.min_value",digits="Meas of Unit")
    min_value2=fields.Float('允许最小值',related="work_sub_id.min_value2",digits="two of Unit")
    max_value=fields.Float('允许最大值',related="work_sub_id.max_value",digits="Meas of Unit")
    max_value2=fields.Float('允许最大值',related="work_sub_id.max_value2",digits="two of Unit")
    avg_val=fields.Float('平均值',digits='Meas of Unit',compute="_compute_avg_val")
    compute_wizard_ids=fields.One2many('meas.compute.wizard','work_sub_wizard_id')
    
    @api.model
    def default_get(self, default_fields):
        result = super(MeasWorkSubWizard,self).default_get(default_fields)
        active_id = self.env.context.get('active_id')
        meas_work_sub=self.env['meas.ctl.work.sub'].browse(active_id)
        real_value=meas_work_sub.real_value
        real_value2=meas_work_sub.real_value2
        decimal=meas_work_sub.decimal

        result['work_sub_id']=active_id
        result['decimal']=decimal
        result['avg_val']=real_value if decimal else real_value2
        return result    

    @api.depends('compute_wizard_ids')
    def _compute_avg_val(self):
        for r in self:
            i=0
            value=0.000
            for rec in r.compute_wizard_ids:
                if rec.value != 0:
                    i=i+1
                    value= value + rec.value
            if i !=0:        
                # r.compute=value/i
                r.avg_val=value/i
            else:
                # r.compute=0
                r.avg_val=0

    def finish_action(self):
        work_sub_id=self.work_sub_id.id
        decimal=self.decimal
        if decimal:
            self.env['meas.ctl.work.sub'].browse(work_sub_id).real_value=self.avg_val
        else:
            self.env['meas.ctl.work.sub'].browse(work_sub_id).real_value2=self.avg_val


class MeasComputeWizard(models.TransientModel):
    _name="meas.compute.wizard"
    _description="采集多数据"
    # _rec_name="value"

    value=fields.Float('采集值',digits="Meas of Unit",default=False)
    work_sub_wizard_id=fields.Many2one('meas.ctl.work.sub.wizard')


# class MeasWorkSub(models.Model):
#     _name="meas.work.sub"
#     _description="工单"
#     _rec_name="number"
#     _order="meas_main_sub_id desc,sequence"

#     datetime_work=fields.Datetime('操作时间',default=fields.Datetime.now())
#     meas_main_sub_id=fields.Many2one('meas.main.sub',string="工序节点号")
#     meas_main_id=fields.Many2one('meas.main',string='工序单号',related="meas_main_sub_id.main_id",store=True)
#     number = fields.Char('节点',related="meas_main_sub_id.number")
#     worktype_selection=fields.Selection(string='类型',related="meas_main_sub_id.worktype_selection",store=True)
#     position_id=fields.Many2one('meas.position',string='位置',related="meas_main_sub_id.position_id",store=True)
#     sequence=fields.Integer(string='顺序号',related="meas_main_sub_id.sequence",store=True)
#     target_value=fields.Float(string="目标值",digits='Meas of Unit',related="meas_main_sub_id.target_value")
#     value_unit=fields.Selection(string='单位',related="meas_main_sub_id.value_unit")
#     min_value=fields.Float(string='最小值',digits='Meas of Unit',related="meas_main_sub_id.min_value")
#     max_value=fields.Float(string='最大值',digits='Meas of Unit',related="meas_main_sub_id.max_value")
#     allow=fields.Float(string='误差范围:',digits='Meas of Unit',related="meas_main_sub_id.allow",store=True)
#     remark=fields.Char("说明",related="meas_main_sub_id.remark")
#     dispatch = fields.Boolean('可下发的工单',related="meas_main_sub_id.dispatch")
#     dispatched = fields.Boolean('已经下发的工单',related="meas_main_sub_id.dispatched")
#     operator_id = fields.Many2one('res.users',string='操作员')
#     company_id = fields.Many2one('res.company',string="组织",related="meas_main_sub_id.company_id",store=True)
#     result=fields.Text('自定义输入')
#     check=fields.Boolean('判断检测')
#     compute_ids=fields.One2many('meas.compute','work_id',string='运算数据',digits="Meas of Unit")
#     real_value=fields.Float('实际数据',digits="Meas of Unit",compute="_compute_compute",readonly=False,store=True)
#     tool_id=fields.Many2one('meas.tool.info',string='所用工具',related="meas_main_sub_id.tool_id",readonly=False)
#     worktype_value=fields.Char('导出数据显示值',related="meas_main_sub_id.worktype_value")
#     require=fields.Text('工艺要求',related="meas_main_sub_id.require")
#     ref=fields.Char('工艺卡文件',related="meas_main_sub_id.ref",store=True)
#     document_image=fields.Image(string="图片文件",max_width=200,max_height=200,related='meas_main_sub_id.document_image')
#     meas_id=fields.Many2one('meas.meas',string='工艺号',related='meas_main_sub_id.meas_id')
#     meas_name=fields.Char('产品名称',related="meas_main_sub_id.meas_name",store=True)
#     no_pass=fields.Boolean('未通过',compute="_compute_no_pass",store=True)
#     prd_id=fields.Many2one('meas.prd',string='机号',related="meas_main_sub_id.prd_id",store=True)
#     prd_num=fields.Char(string='工程编号',related="meas_main_sub_id.prd_num",store=True)

#     @api.depends('operator_id','real_value')
#     def _compute_no_pass(self):
#         for r in self:
#             r.no_pass=True
#             if r.worktype_selection == 'A1' and r.operator_id and r.tool_id:
#                 if r.real_value >= r.min_value and r.real_value <=r.max_value:
#                     r.no_pass=False
#                 else:
#                     r.no_pass=True
#             if r.worktype_selection == 'A2':
#                     r.no_pass=not r.operator_id    
#             if r.worktype_selection == 'A3' and r.operator_id:
#                 if r.result:
#                     r.no_pass=False
#                 else:
#                     r.no_pass=True
#             if r.worktype_selection == 'A4' and r.operator_id and r.tool_id:
#                 # if r.compute >= r.min_value and r.compute <= r.max_value:
#                 if r.real_value >= r.min_value and r.real_value <= r.max_value:
#                     r.no_pass=False
#                 else:
#                     r.no_pass=True

#     def write(self,val):
#         val['operator_id']=self.env.user.id
#         val['datetime_work']=fields.Datetime.now()
#         super().write(val)
    
#     def to_meas_data(self):